package shc.graduation.adapter;

import android.content.Context;
import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.image.ImageInfo;

import org.litepal.crud.DataSupport;

import java.util.List;

import shc.graduation.R;
import shc.graduation.bean.CodeInfo;
import shc.graduation.bean.MyCollection;
import shc.graduation.bean.PaoWangArticle;
import shc.graduation.bean.PaperInfo;
import shc.graduation.utils.DisplayUtil;
import shc.graduation.utils.ServerUtil;
import shc.graduation.view.tagview.Tag;
import shc.graduation.view.tagview.TagView;

/**
 * Created by shuhuiChen on  2016/5/20  18:30.
 * Email ：shuhuiCSH@126.com
 */
public class ModelInfoAdapter extends SimpleBaseAdapter<DataSupport> {
    private static int PROJECT_ID = 0;
    private static final int OPEN_CODE = 0;
    private static final int PAOWANG_ARTICLE = 1;
    private static final int PAPER_ARTICLE = 2;
    private static final int MYCOLLECTION = 3;

    private int mItemPhotoWidth = 0;

    public ModelInfoAdapter(Context context, List<DataSupport> data) {
        super(context, data);
        mItemPhotoWidth = (int) (DisplayUtil.getScreenWidth() * 0.4);
    }

    public void setProject(int projectId) {
        PROJECT_ID = projectId;
    }

    @Override
    public int getItemResource() {

        int layoutId = 0;

        switch (PROJECT_ID) {

            case OPEN_CODE:
                layoutId = R.layout.item_listview_codeinfo;
                break;

            case PAOWANG_ARTICLE:
                layoutId = R.layout.item_listview_paowang;
                break;

            case PAPER_ARTICLE:
                layoutId = R.layout.item_listview_paper;
                break;

            case MYCOLLECTION:
                layoutId = R.layout.item_listview_mycollection;
                break;

            default:
                throw new IllegalStateException("error project state");
        }

        return layoutId;
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    @Override
    public int getItemViewType(int position) {
        return PROJECT_ID;
    }

    @Override
    public View getItemView(int position, View convertView, SimpleBaseAdapter<DataSupport>.ViewHolder holder) {

        switch (PROJECT_ID) {

            case OPEN_CODE:

                codeInfoItemView(position, holder);

                break;

            case PAOWANG_ARTICLE:

                paoWangItemView(position, holder);

                break;

            case PAPER_ARTICLE:

                paperItemView(position, holder);

                break;

            case MYCOLLECTION:

                myCollectionItemView(position, holder);

                break;

            default:
                throw new IllegalStateException("error project state");
        }

        return convertView;
    }

    private void myCollectionItemView(int position, ViewHolder holder) {
        final TextView title, type, time, content;

        title = holder.getView(R.id.item_mycollection_title);
        type = holder.getView(R.id.item_mycollection_type);
        time = holder.getView(R.id.item_mycollection_time);
        content = holder.getView(R.id.item_mycollection_content);
        final SimpleDraweeView simpleDraweeView = (SimpleDraweeView) holder.getView(R.id.item_mycollection_sdv);

        final MyCollection myCollection = (MyCollection) getItem(position);

        title.setText(myCollection.getTitle() + "");

        String typeString = changeToStringType(myCollection.getType());
        type.setText(typeString);

        time.setText(myCollection.getTime());
        content.setText(myCollection.getContent());

        if (!TextUtils.isEmpty(myCollection.getPhotoURL())) {
            Uri uri = Uri.parse(myCollection.getPhotoURL());
            simpleDraweeView.setVisibility(View.VISIBLE);
            ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
                @Override
                public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable anim) {
                    simpleDraweeView.getLayoutParams().width = mItemPhotoWidth;
                    simpleDraweeView.setAspectRatio((float) imageInfo.getWidth() / imageInfo.getHeight());

                }

                @Override
                public void onIntermediateImageSet(String id, @Nullable ImageInfo imageInfo) {
                    simpleDraweeView.getLayoutParams().width = mItemPhotoWidth;
                    simpleDraweeView.setAspectRatio((float) imageInfo.getWidth() / imageInfo.getHeight());
                }

                @Override
                public void onFailure(String id, Throwable throwable) {
                }
            };
            DraweeController controller = Fresco.newDraweeControllerBuilder()
                    .setControllerListener(controllerListener)
                    .setUri(uri)
                    .setAutoPlayAnimations(true)
                    .build();
            simpleDraweeView.setController(controller);
        } else {
            simpleDraweeView.setVisibility(View.GONE);
        }

    }

    private void codeInfoItemView(int position, SimpleBaseAdapter<DataSupport>.ViewHolder holder) {

        final TextView title, description, group, time;

        title = holder.getView(R.id.item_codeinfo_title);
        description = holder.getView(R.id.item_codeinfo_description);
        group = holder.getView(R.id.item_codeinfo_group);
        time = holder.getView(R.id.item_codeinfo_time);
        final SimpleDraweeView simpleDraweeView = (SimpleDraweeView) holder.getView(R.id.item_codeinfo_sdv);

        final CodeInfo codeInfo = (CodeInfo) getItem(position);

        if (!TextUtils.isEmpty(codeInfo.getPhotoUrl())) {
            Uri uri = Uri.parse(ServerUtil.PAOWANG_URL + codeInfo.getPhotoUrl());

            simpleDraweeView.setVisibility(View.VISIBLE);

            ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
                @Override
                public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable anim) {
                    simpleDraweeView.getLayoutParams().width = mItemPhotoWidth;
                    simpleDraweeView.setAspectRatio((float) imageInfo.getWidth() / imageInfo.getHeight());
                }

                @Override
                public void onIntermediateImageSet(String id, @Nullable ImageInfo imageInfo) {
                    simpleDraweeView.getLayoutParams().width = mItemPhotoWidth;
                    simpleDraweeView.setAspectRatio((float) imageInfo.getWidth() / imageInfo.getHeight());
                }

                @Override
                public void onFailure(String id, Throwable throwable) {
                }
            };
            DraweeController controller = Fresco.newDraweeControllerBuilder()
                    .setControllerListener(controllerListener)
                    .setUri(uri)
                    .setAutoPlayAnimations(true)
                    .build();
            simpleDraweeView.setController(controller);
        } else {
            simpleDraweeView.setVisibility(View.GONE);
        }

        title.setText(codeInfo.getName() + "");
        description.setText(codeInfo.getContent() + "");
        group.setText(codeInfo.getGroupName() + "");
        time.setText(codeInfo.getTime() + "");

        //        isCollect.setOnClickListener(new View.OnClickListener() {
        //            @Override
        //            public void onClick(View v) {
        //                //                //已经收藏了
        //                //                if (codeInfo.getCodeCollect() != null) {
        //                //                    CodeCollect codeCollect = codeInfo.getCodeCollect();
        //                //                    codeCollect.delete();
        //                //                    codeInfo.setCodeCollect(null);
        //                //                    //						db.update(codeInfo);
        //                //                    codeInfo.update(codeInfo.getId());
        //                //                    isCollect.setText("未收藏");
        //                //                    isCollect.setTextColor(context.getResources().getColor(R.color.red));
        //                //                } else {//还没收藏呢
        //                //
        //                //                    CodeCollect codeCollect = new CodeCollect();
        //                //                    codeCollect.setCodeInfo(codeInfo);
        //                //                    codeCollect.setCreateTime(String.valueOf(System.currentTimeMillis()));
        //                //                    codeCollect.save();
        //                //
        //                //                    codeInfo.setCodeCollect(codeCollect);
        //                //                    codeInfo.update(codeInfo.getId());
        //                //                    isCollect.setText("已收藏");
        //                //                    isCollect.setTextColor(context.getResources().getColor(R.color.main_color));
        //                //                }
        //
        ////                // 已经收藏了
        ////                if (codeInfo.getMyCollection() != null) {
        ////                    MyCollection myCollection = codeInfo.getMyCollection();
        ////                    myCollection.delete();
        ////                    codeInfo.setMyCollection(null);
        ////                    //						db.update(codeInfo);
        ////                    codeInfo.update(codeInfo.getId());
        ////                    isCollect.setText("未收藏");
        ////                    isCollect.setTextColor(context.getResources().getColor(R.color.red));
        ////                } else {//还没收藏
        ////
        ////                    MyCollection myCollection = new MyCollection();
        ////                    myCollection.setAddressURL(ServerUtil.PAOWANG_URL + codeInfo.getContentUrl());
        ////                    myCollection.setPhotoURL(ServerUtil.PAOWANG_URL + codeInfo.getPhotoUrl());
        ////                    myCollection.setTitle(codeInfo.getName());
        ////                    myCollection.setType(0);
        ////                    myCollection.setTime(codeInfo.getTime());
        ////                    myCollection.setContent(codeInfo.getContent());
        ////                    myCollection.setCodeInfo(codeInfo);
        ////                    myCollection.save();
        ////
        ////                    codeInfo.setMyCollection(myCollection);
        ////                    codeInfo.update(codeInfo.getId());
        ////
        ////                    isCollect.setText("已收藏");
        ////                    isCollect.setTextColor(context.getResources().getColor(R.color.main_color));
        ////                }
        //            }
        //        });
    }

    private void paoWangItemView(int position, SimpleBaseAdapter<DataSupport>.ViewHolder holder) {

        final TextView title, author, time, tag, content;

        title = holder.getView(R.id.item_paowang_title);
        author = holder.getView(R.id.item_paowang_author);
        time = holder.getView(R.id.item_paowang_time);
        tag = holder.getView(R.id.item_paowang_tag);
        content = holder.getView(R.id.item_paowang_content);
        final SimpleDraweeView simpleDraweeView = (SimpleDraweeView) holder.getView(R.id.item_paowang_sdv);

        final PaoWangArticle paoWang = (PaoWangArticle) getItem(position);

        title.setText(paoWang.getTitle() + "");
        author.setText(paoWang.getFromAuthor() + "");
        if (!TextUtils.isEmpty(paoWang.getTagList())) {
            tag.setVisibility(View.VISIBLE);
            tag.setText(paoWang.getTagList() + "");
        } else {
            tag.setVisibility(View.GONE);
        }

        time.setText(paoWang.getPublishDate() + "");
        content.setText(paoWang.getContent() + "");

        if (!TextUtils.isEmpty(paoWang.getPhotoUrl())) {
            Uri uri = Uri.parse(ServerUtil.PAOWANG_URL + paoWang.getPhotoUrl());
            simpleDraweeView.setVisibility(View.VISIBLE);
            ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
                @Override
                public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable anim) {
                    simpleDraweeView.getLayoutParams().width = mItemPhotoWidth;
                    simpleDraweeView.setAspectRatio((float) imageInfo.getWidth() / imageInfo.getHeight());

                }

                @Override
                public void onIntermediateImageSet(String id, @Nullable ImageInfo imageInfo) {
                    simpleDraweeView.getLayoutParams().width = mItemPhotoWidth;
                    simpleDraweeView.setAspectRatio((float) imageInfo.getWidth() / imageInfo.getHeight());
                }

                @Override
                public void onFailure(String id, Throwable throwable) {
                }
            };
            DraweeController controller = Fresco.newDraweeControllerBuilder()
                    .setControllerListener(controllerListener)
                    .setUri(uri)
                    .setAutoPlayAnimations(true)
                    .build();
            simpleDraweeView.setController(controller);
        } else {
            simpleDraweeView.setVisibility(View.GONE);
        }
    }

    private void paperItemView(int position, SimpleBaseAdapter<DataSupport>.ViewHolder holder) {

        final TextView author, paperTime, content;
        TagView tagList;

        author = holder.getView(R.id.item_paper_author);
        paperTime = holder.getView(R.id.item_paper_time);
        tagList = holder.getView(R.id.item_paper_tag);
        tagList.removeAll();
        content = holder.getView(R.id.item_paper_content);
        final SimpleDraweeView contentPhoto = (SimpleDraweeView) holder.getView(R.id.item_paper_photo);
        SimpleDraweeView avatar = holder.getView(R.id.item_paper_avatar);

        final PaperInfo paperInfo = (PaperInfo) getItem(position);

        author.setText(paperInfo.getAuthor() + "");
        paperTime.setText(paperInfo.getPaperTime() + "");
        content.setText(paperInfo.getContent() + "");

        String tagListStr = paperInfo.getTagList();
        if (!TextUtils.isEmpty(tagListStr) && tagListStr.contains(",")) {

            tagList.setVisibility(View.VISIBLE);

            String[] tagArrays = tagListStr.split(",");
            Tag tag = null;
            for (String tagStr : tagArrays) {
                tag = new Tag(tagStr);
                tag.isDeletable = false;
                tagList.add(tag);
            }
        } else {
            tagList.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(paperInfo.getAuthorAvatar())) {
            Uri uri = Uri.parse("http:" + paperInfo.getAuthorAvatar());
            avatar.setVisibility(View.VISIBLE);
            DraweeController controller = Fresco.newDraweeControllerBuilder()
                    .setUri(uri)
                    .setAutoPlayAnimations(true)
                    .build();
            avatar.setController(controller);
        } else {
            avatar.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(paperInfo.getPhotoUrl())) {
            Uri uri = Uri.parse(paperInfo.getPhotoUrl());
            contentPhoto.setVisibility(View.VISIBLE);
            ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
                @Override
                public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable anim) {
                    contentPhoto.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;

                    int height = 500;
                    if (imageInfo.getHeight() < 500) {
                        height = imageInfo.getHeight();
                    }
                    contentPhoto.setAspectRatio((float) imageInfo.getWidth() / height);
                }

                @Override
                public void onIntermediateImageSet(String id, @Nullable ImageInfo imageInfo) {
                    contentPhoto.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;

                    int height = 500;
                    if (imageInfo.getHeight() < 500) {
                        height = imageInfo.getHeight();
                    }
                    contentPhoto.setAspectRatio((float) imageInfo.getWidth() / height);
                    ;
                }

                @Override
                public void onFailure(String id, Throwable throwable) {
                }
            };
            DraweeController controller = Fresco.newDraweeControllerBuilder()
                    .setControllerListener(controllerListener)
                    .setUri(uri)
                    .setAutoPlayAnimations(true)
                    .build();
            contentPhoto.setController(controller);
        } else {
            contentPhoto.setVisibility(View.GONE);
        }
    }

    private String changeToStringType(int type) {
        String typeString = "";
        if (type == 0) {
            typeString = "开源代码";
        } else if (type == 1) {
            typeString = "泡网文章";
        } else {
            typeString = "好东西日报";
        }
        return typeString;
    }

}
